package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.AttractionsComment;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IAttractionsCommentRepository;
import cn.wolfcode.luowowo.comment.repository.IStrategyCommentRepository;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author 猫教授
 * @date 2019/8/13 13:46
 */
@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private IStrategyCommentRepository strategyCommentRepository;

    @Autowired
    private IAttractionsCommentRepository attractionsCommentRepository;

    @Autowired
    private MongoTemplate template;


    @Override
    public void save(StrategyComment comment) {
        strategyCommentRepository.save(comment);

    }

    @Override
    public Long selectCountByDetailId(Long detailId) {
        //strategyCommentRepository.

        //查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("detailId").is(detailId));
        return  template.count(query,StrategyComment.class);

    }

    @Override
    public Page<StrategyComment> selectCommentForList(StrategyCommentQuery qo) {
        //首先先拼接查询条件
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.by(Sort.Direction.ASC, "createTime"));
        query.with(pageable);
        if (qo.getDetailId()!=-1){
            Criteria criteria = Criteria.where("detailId").is(qo.getDetailId());
            query.addCriteria(criteria);
        }
        Long count = template.count(query, StrategyComment.class);
            if (count>0){

            //查询结果
            List<StrategyComment> list = template.find(query,StrategyComment.class,"strategy_comment");
            return new PageImpl(list,pageable,count);
        }else {
            return new PageImpl(Collections.emptyList(),pageable,0);
        }
    }

    @Override
    public void commentThumbUp(String toid, Long userId) {
        //首先把拿出list集合
        Optional<StrategyComment> comment = strategyCommentRepository.findById(toid);
        StrategyComment strategyComment = comment.get();
        if (strategyComment != null) {
            List<Long> thumbuplist = strategyComment.getThumbuplist();
            int thumbupnum = strategyComment.getThumbupnum();
            if (thumbuplist.contains(userId)) {
                thumbuplist.remove(userId);
                strategyComment.setThumbupnum(thumbupnum - 1);
            } else {
                thumbuplist.add(userId);
                strategyComment.setThumbupnum(thumbupnum + 1);
            }
            //然后再更新到数据库
            save(strategyComment);

        }
    }

}
